﻿#include "QWidgetBase.h"
#include "CAppUiConfig.h"
#include <QDebug>

QWidgetBase::QWidgetBase(QWidgetBase *parent)
: QWidget(parent)
{
	initBase();
	m_Parent = parent;
	if(m_Parent != nullptr)
	{
		m_Parent->addChird(this);
	}
}

QWidgetBase::~QWidgetBase()
{
	if(m_Parent == nullptr)
	{
		//onStop();
	} else {
		m_Parent->delChird(this);
	}
	unInitBase();
}

// 创建UI
void QWidgetBase::createUI()
{
	qDebug() << "createUI start";
	setAttribute(Qt::WA_DeleteOnClose,true);
	initUI();
	initStyleSheet();
	initConnect();
	if(m_Parent == nullptr)
	{
		qDebug() << "createUI ------------ onStart start";
		onStart();
		qDebug() << "createUI ------------ onStart stop";
	}
	qDebug() << "createUI stop";
}


// 初始化相关组件
void QWidgetBase::initBase()
{

}

// 释放相关组件
void QWidgetBase::unInitBase()
{
	// 清除所有子控制
	foreach(QWidgetBase *widget,m_QListChirdControl)
	{
	    if(widget)
	    {
	    	m_QListChirdControl.removeOne(widget);
	    }
	}
}

// 添加子节点
void QWidgetBase::addChird(QWidgetBase *parent)
{
	if(parent == nullptr)
		return;
	bool ok = false;
	foreach(QWidgetBase *widget,m_QListChirdControl)
	{
		if(widget == parent)
		{
			ok = true;
			break;
		}
	}
	if(!ok) {
		m_QListChirdControl.append(parent);
	}
}

// 移除子节点
void QWidgetBase::delChird(QWidgetBase *parent)
{

	foreach(QWidgetBase *widget,m_QListChirdControl)
	{
		if(widget == parent)
		{
			if(!m_QListChirdControl.removeOne(widget))
			{
				qDebug("delete error!!!!!");
			}
			break;
		}
	}
}

// 初始化UI
void QWidgetBase::initUI()
{
	this->setWindowFlags(Qt::FramelessWindowHint);

}

// 初始化样式
void QWidgetBase::initStyleSheet()
{

}

// 初始化连接
void QWidgetBase::initConnect()
{

}

// 启动
void QWidgetBase::onStart()
{
	m_isActive = true;
	foreach(QWidgetBase *widget,m_QListChirdControl)
	{
		if(widget != nullptr)
		{
			widget->onStart();
		}
	}
	qDebug() << this->objectName();
}

// 停止
void QWidgetBase::onStop()
{
	m_isActive = false;
	foreach(QWidgetBase *widget,m_QListChirdControl)
	{
		if(widget != nullptr)
		{
			widget->onStop();
		}
	}
}

QWidgetBase* QWidgetBase::getParentWindow()
{
	if(isWindowStatus()){
		return this;
	} else {
		if(m_Parent) {
			return m_Parent->getParentWindow();
		}
	}
	return nullptr;
}

// 启动窗口
void QWidgetBase::startWindow(QWidgetBase *widget)
{
	widget->show();
	widget->setFocus();
	widget->onStart();
}

// 关闭窗口
void QWidgetBase::closeWindow()
{
	QWidgetBase *isThis = getParentWindow();
	QWidgetBase *parent = nullptr;
	if(isThis->m_Parent)
		parent = isThis->m_Parent->getParentWindow();
	if(isThis == nullptr)
	{ 
		isThis = this;
	}
	isThis->onStop();
	if(parent) {
		parent->onStart();
        /*
		// 获取配置文件启动的APP页面
		int statrApp = CConfigUtils::getLocalConfig().startApp;
		//获取镜像中默认启动的APP页面
		int detaultApp = CEntranceRunConfig::getSingleton().getStartAppPage();
		if(statrApp == 1 || (statrApp == 0 && detaultApp == 0))
			QMsgBox::getMsgBoxPtr(parent);
            QMsgBoxLong::Instance(parent);
        */
	}
	isThis->close();
}

// 窗口活跃状态
bool QWidgetBase::isActive()
{
	return m_isActive;
}

// 窗口启动
void QWidgetBase::windowStart()
{
	this->show();
	this->setWindowStatus(true);
}

// 是否为窗口
bool QWidgetBase::isWindowStatus()
{
	return m_IsWindowStatus;
}
// 设置是否为窗口
void QWidgetBase::setWindowStatus(bool status)
{
	m_IsWindowStatus = status;
}

// 截屏
void QWidgetBase::screenshot()
{
	if(!SCREENSHOT)
		return;
	QWidgetBase *isThis = getParentWindow();
	if(!isThis)
		return;
	QPixmap pixmap = isThis->grab();
	if(pixmap.isNull())
		qDebug("截屏失败");
	else
	{
		QDateTime current_date_time =QDateTime::currentDateTime();
		QString filePath = "/data/zjg/image/";
		QString fileName = QString("%1_image.png").arg(current_date_time.toString("yyyy-MM-dd_hh_mm_ss_zzz"));
		//如果路径不存在，则创建
		QDir dir(filePath);
		if(!dir.exists()){
			dir.mkpath(filePath);
		}
		pixmap.save(filePath + fileName);
	}
}

void QWidgetBase::paintEvent(QPaintEvent *event)
{
	QWidget::paintEvent(event);
	Q_UNUSED(event);

	QStyleOption opt;
	opt.init(this);
	QPainter p(this);
	style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

// 重载键盘事件
void QWidgetBase::keyPressEvent(QKeyEvent *event)
{
	QWidget::keyPressEvent(event);
}
